﻿

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;


    public partial class admin_system_navigator_edit : BaseAdmin
    {

        //// Methods
        //private void Display()
        //{
        //    string sql = "Select * From DB_Class Where isnull(shopId,0) =" + base.AdminShopId  + " Order By classorder";
        //    DataTable table = new DataTable();
        //    table = SQLAccess.GreatDs(sql).Tables[0];
        //    if (table.Rows.Count > 0)
        //    {
        //        foreach (DataRow row in table.Rows)
        //        {
        //            this.typeid.Items.Add(new ListItem( row["classname"].ToString() + "", "" + row["classid"].ToString() + ""));
        //        }
        //    }
        //}

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string str2;
            if (this.typeid.SelectedItem.Value.ToString() != "0")
            {
                str2 = this.typeid.SelectedItem.Value.ToString();
            }
            else
            {
                str2 = "0";
            }
            if (str2.ToString() != "0")
            {
                if (base.Request.QueryString["cid"] != null)
                {
                    string sql = "select top 1 * from DB_Class where classid=" + base.Request.QueryString["cid"];
                    DataTable table = new DataTable();
                    table = SQLAccess.GreatDs(sql).Tables[0];
                    if (table.Rows[0]["parentid"].ToString() != str2.ToString())
                    {
                        SQLAccess.DoSql("update DB_Class set amount=amount+1 where classid=" + str2.ToString() + "");
                        SQLAccess.DoSql("update DB_Class set amount=amount-1 where classid=" + table.Rows[0]["parentid"].ToString() + "");
                    }
                }
                else
                {
                    SQLAccess.DoSql("update DB_Class set amount=amount+1 where classid=" + str2.ToString() + "");
                }
            }
            if (base.Request.QueryString["cid"] != null)
            {
                SQLAccess.DoSql(string.Concat(new object[] { 
                "update DB_Class set parentid=", str2.ToString(), ",classname='", re.replace(this.classname.Value), "',classorder=", re.replace(this.classorder.Value), ",visible=",  this.CheckBox1.Checked  ? "1" : "0", ",classlink='", re.replace(this.classlink.Value), "',system="
                ,  this.CheckBox3.Checked  ? "1" : "0", ",classcontent='", this.txtaContent.InnerText.Replace("'", "''"), "',ishead=",   this.CheckBox2.Checked  ? "1" : "0", 
                ",classpcs='", re.replace(this.classpcs.Value), "'",  ",classkey='", re.replace(this.classkey.Value),"'  where classid=", base.Request.QueryString["cid"], ""
             }));
                base.Response.Write("<script language='javascript'>alert('栏目修改成功');window.location.href='navigator.aspx';</script>");
                base.Response.End();
            }
            else
            {
                SQLAccess.DoSql(string.Concat(new object[] { 
                "insert into DB_Class (parentid,classname,classorder,visible,classlink,system,classcontent,classpcs,ishead,shopId,classkey) values (", str2.ToString(), ",'", re.replace(this.classname.Value), "',", re.replace(this.classorder.Value), ",", this.CheckBox1.Checked  ? "1" : "0", ",'", re.replace(this.classlink.Value), "',"
                ,  this.CheckBox3.Checked  ? "1" : "0", ",'",  this.txtaContent.InnerText.Replace("'", "''") , "','", re.replace(this.classpcs.Value), 
                "',",  this.CheckBox2.Checked  ? "1" : "0", ",", base.AdminShopId,",'", this.classkey.Value,  "')"
             }));
                base.Response.Write("<script language='javascript'>alert('栏目添加成功');window.location.href='navigator.aspx';</script>");
                base.Response.End();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.AddMenu);
                if (base.Request.QueryString["cid"] != null)
                {
                    this.upbind();
                }
            }
        }

        private void upbind()
        {
            string sql = "select top 1 * from DB_Class where classid=" + base.Request.QueryString["cid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.typeid.SelectedValue = table.Rows[0]["parentid"].ToString();
            this.txtaContent.InnerText = table.Rows[0]["classcontent"].ToString().Replace("''", "'");
            this.classname.Value = table.Rows[0]["classname"].ToString();
            this.classorder.Value = table.Rows[0]["classorder"].ToString();
            this.classpcs.Value = table.Rows[0]["classpcs"].ToString();
            this.classkey.Value = table.Rows[0]["classkey"].ToString();
            this.classlink.Value = table.Rows[0]["classlink"].ToString();
            this.CheckBox1.Checked = (bool)table.Rows[0]["visible"];
            this.CheckBox2.Checked = (bool)table.Rows[0]["ishead"];
            this.CheckBox3.Checked = (bool)table.Rows[0]["system"];
            if ((bool)table.Rows[0]["system"])
            {
                this.CheckBox3.Enabled = false;
            }
        }


    }